package com.neudu.ssrs_yyzxservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.mapper.BeddetailsMapper;
import com.neudu.ssrs_yyzxservice.mapper.CustomerMapper;
import com.neudu.ssrs_yyzxservice.pojo.Beddetails;
import com.neudu.ssrs_yyzxservice.pojo.Customer;
import com.neudu.ssrs_yyzxservice.pojo.Outward;
import com.neudu.ssrs_yyzxservice.service.BedService;
import com.neudu.ssrs_yyzxservice.service.BeddetailsService;
import com.neudu.ssrs_yyzxservice.service.OutwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BedMapController {
    @Autowired
    JSONReturn jsonReturn;

    @Autowired
    BedService bedService;

    @Autowired
    OutwardService outwardService;

    @Autowired
    BeddetailsService beddetailsService;

    @Autowired
    private CustomerMapper customerMapper;

    @PostMapping("get_customer_data")
    public ResponseEntity<Map<String, Object>> getCustomerData(@RequestBody Map<String, Object> requestBody) {
        try {
            // 获取请求体中的 id 参数
            Integer id = ((Number) requestBody.get("id")).intValue();
            if (id == null) {
                return ResponseEntity.badRequest().body(Map.of(
                        "result", "error",
                        "error", "缺少 id 参数"
                ));
            }

            // 调用 Mapper 获取客户数据
            List<Customer> customers = customerMapper.getCustomerData(id);

            // 返回成功响应
            return ResponseEntity.ok(Map.of(
                    "result", "success",
                    "data", customers
            ));
        } catch (Exception e) {
            // 捕获异常并返回错误响应
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of(
                    "result", "error",
                    "error", e.getMessage()
            ));
        }
    }

    @RequestMapping("get_bed_design")
    public String getBedDesign(@RequestBody(required = false) Map<String, Object> requestParams) {
        try {
            if (requestParams == null || requestParams.isEmpty()) {
                // 没有传入查询条件，返回所有床位列表
                return jsonReturn.returnSuccess(bedService.list());
            }
            String floor = (String) requestParams.get("floor");
            String keyword = (String) requestParams.get("keyword");
            Map<String, Object> queryParams = new HashMap<>();
            if (floor != null && !floor.isEmpty()) {
                queryParams.put("floor", floor);
            }
            if (keyword != null && !keyword.isEmpty()) {
                // 判断 keyword 是房间号还是姓名
                if (isRoomNumber(keyword)) {
                    queryParams.put("roomNo", keyword);
                } else {
                    queryParams.put("customerName", keyword);
                }
            }
            System.out.println(queryParams + "11111111111111111111111");
            // 如果没有提供任何查询条件，至少使用默认的楼层条件
            if (queryParams.isEmpty()) {
                if (floor != null) {
                    queryParams.put("floor", floor);
                } else {
                    return jsonReturn.returnError("至少提供一个查询条件");
                }
            }
            return jsonReturn.returnSuccess(bedService.getBedDesignByConditions(queryParams));
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    private boolean isRoomNumber(String keyword) {
        try {
            Integer.parseInt(keyword);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @PostMapping("/get_outward_info")
    public String getOutwardInfo(@RequestBody Map<String, Object> requestParams) {
        try {
            Integer bedId = (Integer) requestParams.get("bedId");
            if (bedId == null) {
                return jsonReturn.returnError("客户 ID 不能为空");
            }
            List<Outward> outwardList = outwardService.getOutwardByBedId(bedId);
            if (outwardList.isEmpty()) {
                return jsonReturn.returnError("未找到该客户的外出信息");
            } else {
                return jsonReturn.returnSuccess(outwardList);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


}

